<?php

include '../inc/db_util.php';

$action = $_GET['act'];

switch ($action) {
    case 'get':
        read();
        break;
    case 'add':
        create();
        break;
    case 'upd':
        update();
        break;
    case 'del':
        delete();
        break;
    default:
        echo 'Wrong command!';
        break;
}

function read() {
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'kode_matkul';
    $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
    $cari = isset($_POST['cari']) ? mysql_real_escape_string($_POST['cari']) : '';

    $offset = ($page - 1) * $rows;

    $where = " WHERE kode_matkul LIKE '%$cari%' OR matkul LIKE '%$cari%' "
            . " OR sks = '%$cari%' OR semester = '%$cari%' ";

    $query = "SELECT * FROM tb_matkul_master
	$where 
	ORDER BY $sort $order
	LIMIT $rows OFFSET $offset";

    $result = array();
    $result['total'] = mysql_num_rows(mysql_query("SELECT * FROM tb_matkul_master $where"));
    $row = array();

    $criteria = mysql_query($query) or die(mysql_error());

    while ($data = mysql_fetch_array($criteria)) {
        $row[] = array(
            'id_matkul' => $data['id_matkul'],
            'kode_matkul' => $data['kode_matkul'],
            'matkul' => $data['matkul'],
            'sks' => $data['sks'],
            'semester' => $data['semester']
        );
    }
    $result = array_merge($result, array('rows' => $row));
    echo json_encode($result);
}

function create() {
    $kode_matkul = $_POST['kode_matkul'];
    $nama = $_POST['matkul'];
    $sks = $_POST['sks'];
    $semester = $_POST['semester'];

    $row = mysql_num_rows(mysql_query("SELECT * FROM tb_matkul_master WHERE kode_matkul='$kode_matkul'"));

    if ($row < 1) {
        $query = "INSERT INTO tb_matkul_master VALUES(null,'$kode_matkul','$nama','$sks','$semester')";

        $result = mysql_query($query) or die(mysql_error());
        if ($result) {
            echo "$nama telah di simpan";
        }
    } else {
        echo "Kode $kode_matkul sudah ada!";
    }
}

function update() {
    $id_matkul = $_POST['id_matkul'];
    $kode_matkul = $_POST['kode_matkul'];
    $nama = $_POST['matkul'];
    $sks = $_POST['sks'];
    $semester = $_POST['semester'];

    $query = "UPDATE tb_matkul_master SET matkul='$nama', sks='$sks',
                    semester='$semester', kode_matkul='$kode_matkul' 
                    WHERE id_matkul='$id_matkul'";

    $result = mysql_query($query) or die(mysql_error());
    if ($result) {
        echo "$nama telah di update";
    }
}

function delete() {
    $kode_matkul = $_POST['kode_matkul'];

    $array = json_decode($kode_matkul);
    $kode_matkuls = implode("','", $array);

    $query = "DELETE FROM tb_matkul_master WHERE kode_matkul IN ('" . $kode_matkuls . "')";
    $result = mysql_query($query) or die(mysql_error());

    $count = count($array) > 1 ? "(" . count($array) . ") " : "";

    if ($result) {
        echo $count . "Data yang di pilih telah dihapus";
    } else {
        echo "Gagal menghapus " . $count . " data yang di pilih!";
    }
}

?>